<?php
/*
 * kernel/object.session.php
 *
 * This code is distributed and used under the GNU GPL v2 License,
 * please, read file LICENSE, which is situated in root directory,
 * or in "LICENSE" directory with most acceptable translation for you
 *
 * Sessions support 
 *
 * Copyright (C) 2010, Taras Kudla
 *
 */

class gSession extends gKernel implements gSessionStructure {

    /**
     *
     * @var integer
     */
    private $sessionTimeout     = 3600;
    /**
     *
     * @var integer
     */
    public  $sessionLifespan    = 0;
    /**
     *
     * @var string
     */
    private $sessionName        = null;
    /**
     *
     * @var gSessionStructure
     */
    private $sessionHandler     = null;
    /**
     *
     * @var string
     */
    private $sessionUserhash    = null;
    /**
     *
     * @var string
     */
    private $userBrowser        = null;
    /**
     *
     * @var string
     */
    private $userRemoteAddr     = null;
    /**
     *
     * @var string
     */
    private $sessionStarted     = false;

    /**
     *
     * @return boolean
     */
    public function init()
    {
        $this->sessionName = $this->core->get("sessionname");
        $sessionHandler = strtolower($this->core->get("sessionhandler"));
        $this->sessionHandler = $this->getAdapter($sessionHandler);
        // data initialization
        $this->userBrowser = (isset($_SERVER["HTTP_USER_AGENT"]) ? $this->core->escapeString($_SERVER["HTTP_USER_AGENT"]) : "Undefined");
        $this->userRemoteAddr = (isset($_SERVER["REMOTE_ADDR"]) ? $this->core->escapeString($_SERVER["REMOTE_ADDR"]) : "Undefined");
        
        if( isset($_COOKIE[$this->sessionName]) && preg_match("/^([A-Z0-9]{32})$/", $_COOKIE[$this->sessionName]))
        {
            $this->sessionId = $this->core->escapeString($_COOKIE[$this->sessionName]);
        }
        else
        {
            $this->sessionId = strtoupper(md5($this->userRemoteAddr.$this->userBrowser.microtime(true)));
            setcookie($this->sessionName, $this->sessionId, $this->sessionLifespan, "/");
        }
        $this->sessionUserhash = md5($this->userBrowser.$this->userRemoteAddr);
        $this->sessionHandler->init($this->sessionId,$this->sessionUserhash);
        
    }

    /**
     *
     * @return <type>
     */
    public function start(){
        if( $this->sessionStarted){
            return;
        } else {
            $this->sessionStarted = true;
            return $this->sessionHandler->start();
        }
    }
    /**
     *
     * @return <type>
     */
    public function getSessionId(){
        return $this->sessionId;
    }
    /**
     *
     * @return <type>
     */
    public function getSessionLifespan(){
        return $this->sessionLifespan;
    }
    /**
     *
     * @return <type>
     */
    public function getUserBrowser(){
        return $this->userBrowser;
    }
    /**
     *
     * @return <type>
     */
    public function getSessionTimeout(){
        return $this->sessionTimeout;
    }
    /**
     *
     * @return <type>
     */
    public function getSessionName(){
        return $this->sessionName;
    }
    /**
     *
     * @param <type> $key
     * @param <type> $value
     */
    public function set($key,$value=null){
        $this->sessionHandler->set($key,$value);
    }
    /**
     *
     * @param <type> $key
     * @return <type>
     */
    public function get($key){
        return $this->sessionHandler->get($key);
    }
    /**
     *
     * @param <type> $key
     * @return <type>
     */
    public function delete($key){
        return $this->sessionHandler->delete($key);
    }
    /**
     *
     * @param <type> $key
     * @return <type>
     */
    public function exists($key) {
        return $this->sessionHandler->exists($key);
    }
    /**
     *
     * @return <type>
     */
    public function  write() {
        return $this->sessionHandler->write();
    }
    /**
     *
     * @return <type>
     */
    public function  read() {
        return $this->sessionHandler->read();
    }
    /**
     *
     * @param <type> $sessionId
     * @return <type>
     */
    public function  clear($sessionId) {
        return $this->sessionHandler->clear($sessionId);
    }
    /**
     *
     * @return <type>
     */
    public function  close() {
        return $this->sessionHandler->close();
    }
    /**
     *
     * @return <type>
     */
    public function  destroy() {
        return $this->sessionHandler->destroy();
    }
    /**
     *
     * @return <type>
     */
    public function  collectgarbage() {
        return $this->sessionHandler->collectgarbage();
    }
    /**
     *
     * @param <type> $time
     * @return <type> 
     */
    public function resetLifetime($time){
        return $this->sessionHandler->resetLifetime($time);
    }
}